package com.tij;

import java.util.ArrayList;

/**
 * @ClassName:处理简单类的容器
 * @author: xyj
 * @Description: TODO
 * data:2024/10/2 12:10
 * @since
 **/
public class ApplesAndOrangesWithoutGennerics {
    public static void main(String[] args) {
        // 默认ArrayList中保存的是Object对象
        ArrayList  apples= new ArrayList();
        for(int i =0;i <3;i++){
            apples.add(new Apple());
        }
        apples.add(new Orange());


        for(int i=0;i<apples.size();i++){
            /**
            * Exception in thread "main" java.lang.ClassCastException: com.tij.Orange cannot be cast to com.tij.Apple
             *类型转换异常
            **/
            System.out.println( ((Apple)apples.get(i)).id());
        }
    }

}

class Apple {
    //static 表示内存只有一块区间，class使用，非对象使用
    private static long counter;
    //final 表示不可以被修改,
    private final long id = counter ++;
    public Apple(){
        System.out.println("counter:"+ counter + "  " + System.identityHashCode(counter));
        System.out.println("id:"+ id + "  " + System.identityHashCode(id));

    }
    public long id(){
        return id;
    }

}

class Orange{}